package org.example;

import io.restassured.RestAssured;
import static io.restassured.RestAssured.given;
import io.restassured.specification.ProxySpecification;
import org.junit.Test;

/*
* 代理
* 1. 打开charles等代理工具, 设置正确(port=8888)
* 2. 执行代码
**/
public class ProxySetting {
    public static String getHttpUrl = "http://httpbin.org/get";
    public static String getHttpsUrl = "https://httpbin.ceshiren.com/get";

    @Test
    public void proxyHttp1() {
        given()
                .proxy("localhost", 8888) // 设置代理方式一
                //.proxy("127.0.0.1", 8888)
        .when()
                .get(getHttpUrl)
        .then()
                .statusCode(200)
                .log().all();
    }

    @Test
    public void proxyHttp2() {
        given()
                .proxy(8888) // 设置代理方式二
        .when()
                .get(getHttpUrl)
        .then()
                .statusCode(200)
                .log().all();
    }

    @Test
    public void proxyHttp3() {
        RestAssured.proxy = ProxySpecification.host("localhost").withPort(8888); // 设置代理方式三

        given()
        .when()
                .get(getHttpUrl)
        .then()
                .statusCode(200)
                .log().all();
    }

    @Test
    public void proxyIgnoreAssertHttp1() {
        // 忽略https校验 == python requests中的verify=False
        given()
                .proxy(8888)
                .relaxedHTTPSValidation() // 忽略https校验方式一
        .when()
                .get(getHttpsUrl)
        .then()
                .statusCode(200)
                .log().all();
    }

    @Test
    public void proxyIgnoreAssertHttp2() {
        // 忽略https校验 == python requests中的verify=False
        RestAssured.proxy = RestAssured.proxy = ProxySpecification.host("localhost").withPort(8888); // 设置代理
        RestAssured.useRelaxedHTTPSValidation(); // 忽略https校验方式二

        given()
                .header("Content-Type", "application/json", "hello", "java")
        .when()
                .get(getHttpsUrl)
        .then()
                .statusCode(200)
                .log().all();
    }
}
